

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;


public class WebService {

        public enum Result {
                SUCCESSED, FAILED, EXCEPTION;
        }
        
        // Kiem tra thong tin dang nhap cua  nguoi dung tren webserver bang webservices
        public static String checkLogin(String username, String password) throws Exception {
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/CheckLogin";
            String METHOD_NAME = "CheckLogin";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("username", username);
            request.addProperty("password", password);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);              
            androidHttpTransport.call(SOAP_ACTION, envelope);                            
            /*---hoan tat gui yeu cau len server---*/
            /*---nhan ket qua tra ve tu server---*/
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
            String myResult = result.toString().trim();
            return myResult;
        }

        // Tao tai khoan moi cho nguoi dung
        public static String createAccount(String username, String password,String email) throws Exception {
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/CreateAccount";
            String METHOD_NAME = "CreateAccount";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("username", username);
            request.addProperty("password", password);
            request.addProperty("email", email);
            request.addProperty("note", "");
            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);            
            
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            String myResult = result.toString().trim();
            return myResult;
        }
        
        
//        public static ArrayList<FileObject> getAllContactList(int userid)throws Exception{
//            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/getAllContactList";
//            String METHOD_NAME = "getAllContactList";
//            String NAMESPACE = "http://vn.edu.uit";
//            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
//            
//            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//            request.addProperty("userID", userid);
//            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
//            soapEnvelope.setOutputSoapObject(request);
//            soapEnvelope.addMapping(NAMESPACE, "FileObject", new FileObject() {
//				
//				@Override
//				public Object getProperty(int arg0) {
//					// TODO Auto-generated method stub
//					return null;
//				}
//			}.getClass());  
//    		FileObject f= new FileObject() {
//			};
//			PropertyInfo pii = new PropertyInfo();
//			pii.setName("f");
//			pii.setValue(f);
//			pii.setType(f.getClass());
//			request.addProperty(pii);
//            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
//            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
//
////    		SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyOut;
//    		SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
////            KvmSerializable resultsRequestSOAP = (KvmSerializable)soapEnvelope.bodyIn;
////    		SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.getResponse();
//    		
//    		int numberOfItem = resultsRequestSOAP.getPropertyCount();    		
//    		ArrayList<FileObject> listFile = new  ArrayList<FileObject>();
//    		
//            for(int i = 0; i < numberOfItem; i++){
//    			SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(i);
//
//				f.setFileID(Integer.parseInt(sObject.getProperty("fileID").toString()));
//				f.setUserID(Integer.parseInt(sObject.getProperty("userID").toString()));
//				f.setFileName(sObject.getProperty("fileName").toString().trim());
//				f.setFileType(Integer.parseInt(sObject.getProperty("fileType").toString()));
//				f.setFileSize(Integer.parseInt(sObject.getProperty("fileSize").toString()));
//				f.setPathFile(sObject.getProperty("pathFile").toString().trim());
//				f.setStatusFile(Integer.parseInt(sObject.getProperty("statusFile").toString()));
//				f.setNote(sObject.getProperty("note").toString().trim());
//				f.setDayBackup(sObject.getProperty("dayBackup").toString().trim());
//				f.setDayDelete(sObject.getProperty("dayDelete").toString().trim());
//				
//    			listFile.add(f);
//            }
//            return listFile;        	
//        	
//        }
           
        
        public static ArrayList<FileObject> getAllContactList(int userid)throws Exception{
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/getAllContactList";
            String METHOD_NAME = "getAllContactList";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userID", userid);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            soapEnvelope.setOutputSoapObject(request);
            soapEnvelope.addMapping(NAMESPACE, "FileObject", new FileObject() {}.getClass());
            FileObject ff= new FileObject() {};
			PropertyInfo pii = new PropertyInfo();
			pii.setName("ff");
			pii.setValue(ff);
			pii.setType(ff.getClass());
			request.addProperty(pii);
			
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
            ArrayList<FileObject> listFile = new  ArrayList<FileObject>();
            try{
            	SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
            	int numberOfItem = resultsRequestSOAP.getPropertyCount();
            	
            	for(int i = 0; i < numberOfItem; i++){
            		System.out.println("i++ "+i);
            		SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(i);
     				int fileID = Integer.parseInt(sObject.getProperty("fileID").toString());
     				int userID = Integer.parseInt(sObject.getProperty("userID").toString());
     				String fileName = sObject.getProperty("fileName").toString().trim();
     				int fileType = Integer.parseInt(sObject.getProperty("fileType").toString());
     				int fileSize = Integer.parseInt(sObject.getProperty("fileSize").toString());
     				String pathFile = sObject.getProperty("pathFile").toString().trim();
     				int statusFile = Integer.parseInt(sObject.getProperty("statusFile").toString());
     				String Note = sObject.getProperty("note").toString().trim();
     				String dayBackup = sObject.getProperty("dayBackup").toString().trim();
     				String dayDelete = sObject.getProperty("dayDelete").toString().trim();
     				listFile.add( new FileObject(fileID,userID,fileName,fileType,fileSize,pathFile,statusFile,dayBackup,dayDelete,Note) {});
            	}
            	return listFile; 
            }catch(Exception e){
            	System.out.println("error when you try to "+e.toString());
            }    		    		    		    		
    		return null;    		      	        
        }
        
        public static FileObject getAllList(int userid)throws Exception{
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/getAllList";
            String METHOD_NAME = "getAllList";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userID", userid);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            soapEnvelope.setOutputSoapObject(request);			
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
            SoapObject resultsRequestSOAP = null;
            FileObject f = new FileObject() {};
            try{
            	resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;         	 
     			SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(0);
     			
 				f.setFileID(Integer.parseInt(sObject.getProperty("fileID").toString()));
 				f.setUserID(Integer.parseInt(sObject.getProperty("userID").toString()));
 				f.setFileName(sObject.getProperty("fileName").toString().trim());
 				f.setFileType(Integer.parseInt(sObject.getProperty("fileType").toString()));
 				f.setFileSize(Integer.parseInt(sObject.getProperty("fileSize").toString()));
 				f.setPathFile(sObject.getProperty("pathFile").toString().trim());
 				f.setStatusFile(Integer.parseInt(sObject.getProperty("statusFile").toString()));
 				f.setNote(sObject.getProperty("note").toString().trim());
 				f.setDayBackup(sObject.getProperty("dayBackup").toString().trim());
 				f.setDayDelete(sObject.getProperty("dayDelete").toString().trim());
            }catch(Exception e){
            	System.out.println("error when you try to "+e.toString());
            }  		    		
    		return f;    		      	        
        }

        
        public static ArrayList<FileObject> getAllSMSList(int userid)throws Exception{
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/getAllSMSList";
            String METHOD_NAME = "getAllSMSList";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userID", userid);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            soapEnvelope.setOutputSoapObject(request);
            soapEnvelope.addMapping(NAMESPACE, "FileObject", new FileObject() {}.getClass());
            FileObject ff= new FileObject() {};
			PropertyInfo pii = new PropertyInfo();
			pii.setName("ff");
			pii.setValue(ff);
			pii.setType(ff.getClass());
			request.addProperty(pii);
			
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
            ArrayList<FileObject> listFile = new  ArrayList<FileObject>();
            try{
            	SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
            	int numberOfItem = resultsRequestSOAP.getPropertyCount();
            	
            	for(int i = 0; i < numberOfItem; i++){
            		System.out.println("i++ "+i);
            		SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(i);
     				int fileID = Integer.parseInt(sObject.getProperty("fileID").toString());
     				int userID = Integer.parseInt(sObject.getProperty("userID").toString());
     				String fileName = sObject.getProperty("fileName").toString().trim();
     				int fileType = Integer.parseInt(sObject.getProperty("fileType").toString());
     				int fileSize = Integer.parseInt(sObject.getProperty("fileSize").toString());
     				String pathFile = sObject.getProperty("pathFile").toString().trim();
     				int statusFile = Integer.parseInt(sObject.getProperty("statusFile").toString());
     				String Note = sObject.getProperty("note").toString().trim();
     				String dayBackup = sObject.getProperty("dayBackup").toString().trim();
     				String dayDelete = sObject.getProperty("dayDelete").toString().trim();
     				listFile.add( new FileObject(fileID,userID,fileName,fileType,fileSize,pathFile,statusFile,dayBackup,dayDelete,Note) {});
            	}
            	return listFile; 
            }catch(Exception e){
            	System.out.println("error when you try to "+e.toString());
            }    		    		    		    		
    		return null;    		      	        
        }
        
        
        public static ArrayList<FileObject> getAllFileList(int userid)throws Exception{
            String SOAP_ACTION = "http://localhost:8080/ServerWeb/services/Service/getAllFileList";
            String METHOD_NAME = "getAllFileList";
            String NAMESPACE = "http://vn.edu.uit";
            String URL = "http://localhost:8080/ServerWeb/services/Service?wsdl";
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userID", userid);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            soapEnvelope.setOutputSoapObject(request);
            soapEnvelope.addMapping(NAMESPACE, "FileObject", new FileObject() {}.getClass());
            FileObject ff= new FileObject() {};
			PropertyInfo pii = new PropertyInfo();
			pii.setName("ff");
			pii.setValue(ff);
			pii.setType(ff.getClass());
			request.addProperty(pii);
			
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
            ArrayList<FileObject> listFile = new  ArrayList<FileObject>();
            try{
            	SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
            	int numberOfItem = resultsRequestSOAP.getPropertyCount();
            	
            	for(int i = 0; i < numberOfItem; i++){
            		System.out.println("i++ "+i);
            		SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(i);
     				int fileID = Integer.parseInt(sObject.getProperty("fileID").toString());
     				int userID = Integer.parseInt(sObject.getProperty("userID").toString());
     				String fileName = sObject.getProperty("fileName").toString().trim();
     				int fileType = Integer.parseInt(sObject.getProperty("fileType").toString());
     				int fileSize = Integer.parseInt(sObject.getProperty("fileSize").toString());
     				String pathFile = sObject.getProperty("pathFile").toString().trim();
     				int statusFile = Integer.parseInt(sObject.getProperty("statusFile").toString());
     				String Note = sObject.getProperty("note").toString().trim();
     				String dayBackup = sObject.getProperty("dayBackup").toString().trim();
     				String dayDelete = sObject.getProperty("dayDelete").toString().trim();
     				listFile.add( new FileObject(fileID,userID,fileName,fileType,fileSize,pathFile,statusFile,dayBackup,dayDelete,Note) {});
            	}
            	return listFile; 
            }catch(Exception e){
            	System.out.println("error when you try to "+e.toString());
            }    		    		    		    		
    		return null;    		      	        
        }
        
}

